/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.rational.sheet.table;

import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableModel;
import org.rational.sheet.table.cell.model.CellValue;
import org.rational.sheet.table.cell.view.RationalTableCellEditor;
import org.rational.sheet.table.cell.view.RationalTableCellRenderer;
import org.rational.sheet.table.adapter.ClipborardTableAdapter;
import org.rational.sheet.table.adapter.RemoveTableAdapter;
import org.rational.sheet.table.column.ColumnHeaderAdapter;
import org.rational.sheet.table.row.RowHeader;

/**
 *
 * @author Overmind
 */
public final class RationalTable
    extends JTable
{   
    protected ClipborardTableAdapter clipborardTableAdapter;
    protected RemoveTableAdapter removeTableAdapter;
    protected ColumnHeaderAdapter columnHeaderAdapter;
    protected RowHeader rowHeader;
    
    public RationalTable(RationalTableData tableData)
    {
	super(tableData);
	
	setDefaultRenderer(CellValue.class, new RationalTableCellRenderer());
	setDefaultEditor(CellValue.class, new RationalTableCellEditor());
        
        setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
	setCellSelectionEnabled(true);
	setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
	getTableHeader().setReorderingAllowed(false);
	
        clipborardTableAdapter = new ClipborardTableAdapter(this);
	removeTableAdapter = new RemoveTableAdapter(this);
	
        columnHeaderAdapter = new ColumnHeaderAdapter(this);
	rowHeader = new RowHeader(this);
    }

    public RationalTableData getTableData()
    {
	return (RationalTableData)getModel();
    }
    
    public RationalTableCellEditor getTableCellEditor()
    {
	return (RationalTableCellEditor)super.getDefaultEditor(CellValue.class);
    }
    
    public RationalTableCellRenderer getTableCellRenderer()
    {
	return (RationalTableCellRenderer)super.getDefaultRenderer(CellValue.class);
    }
    
    @Override
    public void setModel(TableModel model)
    {
	try
	{
	    super.setModel((RationalTableData)model);
	}
	catch(ClassCastException cce)
	{
	    throw new ClassCastException("model most be instanseof RationalTableData");
	}
    }

    @Override
    public void setDefaultEditor(Class<?> type, TableCellEditor cellEditor)
    {
	try
	{
	    super.setDefaultEditor((Class<CellValue>)type, (RationalTableCellEditor)cellEditor);
	}
	catch(ClassCastException cce)
	{
	    throw new ClassCastException("cellEditor most be instanseof RationalTableCellEditor");
	}
	
    }
    
    @Override
    public void setDefaultRenderer(Class<?> type, TableCellRenderer cellRenderer)
    {
	try
	{
	    super.setDefaultRenderer((Class<CellValue>)type, (RationalTableCellRenderer)cellRenderer);
	}
	catch(ClassCastException cce)
	{
	    throw new ClassCastException("cellRenderer most be instanseof RationalTableCellRenderer");
	}
	
    }
    
    public boolean isOneCellSelected()
    {
	return (getSelectedRowCount() == 1) &&
		(getSelectedColumnCount() == 1);
    }
    
    @Override
    public boolean isCellEditable(int row, int column)
    {
        return isOneCellSelected() && getModel().isCellEditable(row, column);
    }
    
    protected ClipborardTableAdapter getClipborardTableAdapter()
    {
	return clipborardTableAdapter;
    }
    
    protected RemoveTableAdapter getRemoveTableAdapter()
    {
	return removeTableAdapter;
    }
}
